﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Physis.Client.Controllers {

    public class EnterpriseManagementController : ApplicationController {

        protected override void Initialize (System.Web.Routing.RequestContext requestContext) {

            base.Initialize (requestContext);

            if (!applicationModel.Application.Session.HasEnterprisePermission (Physis.Security.Permissions.Enterprise.EnterpriseManagement)) {

                Response.Redirect ("~/PermissionDenied", true);

            }

            return;

        }


        #region Security Authority

        [HttpGet]
        public ActionResult SecurityAuthority (Int64 id) {

            Models.Configuration.SecurityAuthorityModel model = new Models.Configuration.SecurityAuthorityModel (id);

            return View ("~/Views/Configuration/SecurityAuthority.cshtml", model);

        }

        [HttpPost]
        public JsonResult SecurityAuthoritySave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Configuration.SecurityAuthorityModel model = new Models.Configuration.SecurityAuthorityModel (Request.Form);

            response.Result = model.SecurityAuthority.Save ();

            response.Id = model.SecurityAuthority.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult SecurityAuthorityDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Configuration.SecurityAuthorityModel model = new Models.Configuration.SecurityAuthorityModel (id);

            response.Result = model.SecurityAuthority.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 


        #region Environment 
        
        [HttpGet]
        public ActionResult Environment (Int64 id) {

            Models.Configuration.EnvironmentModel model = new Models.Configuration.EnvironmentModel (id);

            return View ("~/Views/Configuration/Environment.cshtml", model);

        }

        [HttpPost]
        public JsonResult EnvironmentSave () {

            JsonResponses.SaveResponse response = new JsonResponses.SaveResponse ();

            Models.Configuration.EnvironmentModel model = new Models.Configuration.EnvironmentModel (Request.Form);

            response.Result = model.Environment.Save ();

            response.Id = model.Environment.Id;

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        [HttpDelete]
        public JsonResult EnvironmentDelete (Int64 id) {

            JsonResponses.BooleanResponse response = new JsonResponses.BooleanResponse ();

            Models.Configuration.EnvironmentModel model = new Models.Configuration.EnvironmentModel (id);

            response.Result = model.Environment.Delete ();

            response.SetException (model.Application.LastException);

            return Json (response, JsonRequestBehavior.DenyGet);

        }

        #endregion 



    }

}
